<template>
  <div class="AdendDialog">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="22">
          <el-form-item label="原密码" prop="password">
            <el-input clearable v-model="ruleForm.password" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="新密码" prop="newPassword">
            <el-input clearable v-model="ruleForm.newPassword" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="22">
          <el-form-item label="重复密码" prop="checkPass">
            <el-input clearable v-model="ruleForm.checkPass" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <!-- <el-button type="primary" @click="resetPassword">重置密码</el-button> -->
        <el-popconfirm placement="top" hide-icon title="是否重置密码为111111？" @onConfirm="resetPassword()">
          <el-button style="margin-right: 10px;" slot="reference" type="primary">重置密码</el-button>
        </el-popconfirm>
        <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        <el-button @click="resetForm('ruleForm')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    props: ["row"],
    data() {
      var validatePass = (rule, value, callback) => {
        console.log(value);
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if (value.length < 6) {
          callback(new Error('密码不能少于6位'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          password: '',
          newPassword: '',
          checkPass: '',
        },
        rules: {
          password: [{
            required: true,
            message: '请输入原密码',
            trigger: 'blur'
          }, ],
          newPassword: [{
            required: true,
            message: '请输入新密码',
            trigger: 'blur'
          }, {
            validator: validatePass,
            trigger: 'blur'
          }],
          checkPass: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }, {
            validator: validatePass2,
            trigger: 'blur'
          }],
        },
        orgTree: [],
      }
    },
    created: function() {},
    methods: {
      // 重置密码
      resetPassword: function() {
        let params = {
          userId: this.row.userId,
          password: '111111'
        }
        this.$server.editSysUser(params).then(res => {
          this.$message({
            type: "success",
            message: "重置成功!"
          });
          // 关闭弹框
          this.$emit('chang');
        })
      },
      // 表单提交
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let params = {
              password: this.ruleForm.password,
              newPassword: this.ruleForm.newPassword,
              userId: this.row.userId,
            }
            this.$server.editSysUserPassword(params).then(res => {
              this.$message({
                type: "success",
                message: "添加成功!"
              });
              // 关闭弹框
              this.$emit('chang');
            })
          } else {
            // console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$emit('chang');
      },
    }
  }
</script>
<style lang="scss" scoped>
  .AdendDialog {}
</style>
